<template>
	<div>
		<el-form ref="form" :model="role" label-width="80px">
			<el-form-item label="角色名称">
				<el-input v-model="role.name"></el-input>
			</el-form-item>
			<el-form-item label="角色编码">
				<el-input v-model="role.code"></el-input>
			</el-form-item>
			<el-form-item label="描述">
				<el-input
					type="textarea"
					rows="5"
					v-model="role.description"
				></el-input>
			</el-form-item>
			<el-form-item class="dialog-footer">
				<el-button @click="$emit('cancel')">取 消</el-button>
				<el-button type="primary" @click="dialogSubmit">确 定</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script lang="ts">
import Vue from 'vue'
import { createOrUpdate, getRoleById } from '@/services/role'

export default Vue.extend({
  name: 'CreateOrEdit',
  props: {
    roleId: {
      type: [String, Number]
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      role: {
        name: '',
        code: '',
        description: ''
      }
    }
  },
  created () {
    // 如果是编辑则加载角色信息
    if (this.isEdit) {
      this.loadRole()
    }
  },
  methods: {
    async dialogSubmit () {
      await createOrUpdate(this.role)
      this.$message.success('操作成功')
      this.$emit('success')
    },
    async loadRole () {
      const { data } = await getRoleById(this.roleId)
      this.role = data.data
    }
  }
})
</script>

<style lang="sass" scoped></style>
